package com.dk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.dk.mapper.MenuMapper;
import com.dk.pojo.Menu;
import com.dk.pojo.MenuExample;
import com.dk.pojo.User;
import com.dk.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<Map<String, Object>> getMenu(User user) {
		//调用mapper查询出所有的菜单数据
		//List<Menu> menuList = menuMapper.selectByExample(new MenuExample());
		//根据用户id查询用户所拥有的角色
		List<Menu> menuList = menuMapper.selectMenuByUserId(user.getUid());
		//使用递归拼接树形结构
		List<Map<String,Object>> menuTree = getMenuTree(menuList,0);
		return menuTree;
	}

	public List<Map<String, Object>> getMenuTree(List<Menu> menuList,Integer pid){
		List<Map<String, Object>> listTree = new ArrayList<>();
		for (int i = 0; i < menuList.size(); i++) {
			Map<String, Object> map = null;
			Menu menu = menuList.get(i);
			if (menu.getPid().equals(pid)) {
				map = new HashMap();
				map.put("id", menu.getId());
				map.put("text", menu.getText());
				map.put("selectedIcon", menu.getSelectedicon());
				map.put("icon", menu.getIcon());
				map.put("color", menu.getColor());
				map.put("backColor", menu.getBackcolor());
				map.put("url", menu.getUrl());
				map.put("href", menu.getHref());
				map.put("selectable", Boolean.parseBoolean(menu.getSelectable()));
				map.put("nodes", getMenuTree(menuList,menu.getId()));
			}
			if (map != null) {
				List<Map<String, Object>> nodes = (List<Map<String, Object>>)map.get("nodes");
				if (nodes.size() == 0) {
					map.remove("nodes");
					map.remove("selectable");
				}
				listTree.add(map);
			}
		}
		return listTree;
	}

	@Override
	public List<Map<String, Object>> getZTree() {
		List<Menu> menuList = menuMapper.selectByExample(new MenuExample());
		List<Map<String,Object>> zTree = getZTree(menuList,0);
		return zTree;
	}

	public List<Map<String, Object>> getZTree(List<Menu> menuList,Integer pid){
		List<Map<String, Object>> listTree = new ArrayList<>();
		for (int i = 0; i < menuList.size(); i++) {
			Map<String, Object> map = null;
			Menu menu = menuList.get(i);
			if (menu.getPid().equals(pid)) {
				map = new HashMap();
				map.put("id", menu.getId());
				map.put("name", menu.getText());
				map.put("selectedIcon", menu.getSelectedicon());
				map.put("icon", menu.getIcon());
				map.put("color", menu.getColor());
				map.put("backColor", menu.getBackcolor());
				map.put("url", menu.getUrl());
				map.put("href", menu.getHref());
				map.put("open", Boolean.parseBoolean(menu.getSelectable()));
				//map.put("checked", true);
				map.put("children", getZTree(menuList,menu.getId()));
			}
			if (map != null) {
				List<Map<String, Object>> nodes = (List<Map<String, Object>>)map.get("children");
				if (nodes.size() == 0) {
					map.remove("children");
					map.remove("open");
				}
				listTree.add(map);
			}
		}
		return listTree;
	}

	@Override
	public void addMenu(Menu menu) {
		// TODO Auto-generated method stub
		menuMapper.insertSelective(menu);
	}

	@Override
	public Menu findMenuById(Integer id) {
		// TODO Auto-generated method stub
		return menuMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateMenu(Menu menu) {
		// TODO Auto-generated method stub
		menuMapper.updateByPrimaryKeySelective(menu);
	}

	@Override
	public void deleteMenu(Integer id) {
		// TODO Auto-generated method stub
		menuMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Map<String, Object>> getCheckedZTree(Integer roleId) {
		//��ѯȨ��������Ȩ��
		List<Menu> menuList = menuMapper.selectByExample(new MenuExample());
		//��ɫȨ�����飬��ѯ����ǰ��ɫ��ӵ�е�Ȩ�ޣ���ѯȨ��id
		List<Integer> menuId = menuMapper.selectByRoleId(roleId);
		//�Աȵ�ǰ��ɫ��ӵ�е�Ȩ�ޣ�����оʹ�
		List<Map<String, Object>> ckTree = getCheckedZTree(menuList,0,menuId);
		return ckTree;
	}
	//ƴ�ӻ��Ե���
	public List<Map<String, Object>> getCheckedZTree(List<Menu> menuList,Integer pid,List<Integer> menuId){
		//����һ�����Ͻ��շ��ؽ��
		List<Map<String, Object>> listTree = new ArrayList<>();
		//�������ݿ��ѯ�����,��װ����
		for (int i = 0; i < menuList.size(); i++) {
			//����һ��map������װ���
			Map<String, Object> map = null;
			Menu menu = menuList.get(i);
			if (menu.getPid().equals(pid)) {
				map = new HashMap();
				map.put("id", menu.getId());
				map.put("name", menu.getText());
				map.put("selectedIcon", menu.getSelectedicon());
				map.put("icon", menu.getIcon());
				map.put("color", menu.getColor());
				map.put("backColor", menu.getBackcolor());
				map.put("url", menu.getUrl());
				map.put("href", menu.getHref());
				map.put("open", Boolean.parseBoolean(menu.getSelectable()));
				//��ǰ���ϰ���ĳ����
				if (menuId != null && menuId.size() > 0 && menuId.contains(menu.getId())) {
					map.put("checked", true);
				}
				//��һ������ ���ݿ���     �ڶ�������  ��ǰ�����idֵ
				map.put("children", getCheckedZTree(menuList,menu.getId(),menuId));
			}
			if (map != null) {
				//���map���nodesΪ�գ��Ͱ��������ɾ��
				List<Map<String, Object>> nodes = (List<Map<String, Object>>)map.get("children");
				if (nodes.size() == 0) {
					map.remove("children");
					map.remove("open");
				}
				listTree.add(map);
			}
		}
		return listTree;
	}
	

}
